﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text.RegularExpressions;
using System.Web;
using AmuTools;
using app.Models;

namespace app.Api
{
    public class Permission
    {
        /*
            权限格式(X为0-9的数字，模块码和操作码第一位不能为0)：
            附加模块操作权限：+[XXXX][XX] 加号+模块码+操作码
            禁用模块操作权限：-[XXXX][XX] 减号+模块码+操作码

            权限分用户权限和角色权限，用户权限高于角色权限
            用户权限有禁用权限，但是角色没有禁用权限
            超级管理员权限为+100010，不可更改
        */

        static public PermissionOperation Module(PERMISSIONMODULE module)
        {
            return new PermissionOperation(module);
        }

        // 检测是否有与权限码对应的权限，需要提供权限字符串、权限码作为参数
        static public bool Check(string permission, string code)
        {
            Regex r;
            Match m;

            // 检测是否为超级管理员
            r = new Regex(@"\+100010");
            m = r.Match(permission);
            if (m.Value != "") return true;

            // 禁用模块组检测
            r = new Regex(string.Format(@"\-{0}10", code.Substring(0, 4)));
            m = r.Match(permission);
            if (m.Value != "") return false;

            // 禁用权限检测
            r = new Regex(string.Format(@"\-{0}", code));
            m = r.Match(permission);
            if (m.Value != "") return false;

            // 附加模块组检测
            r = new Regex(string.Format(@"\+{0}10", code.Substring(0, 4)));
            m = r.Match(permission);
            if (m.Value != "") return true;

            // 附加权限检测
            r = new Regex(string.Format(@"\+{0}", code));
            m = r.Match(permission);
            if (m.Value != "") return true;

            return false;
        }

        // 将权限码添加到权限中，返回新的权限
        static public string Add(string permission, string code)
        {
            // 检测是否为超级管理员
            if (permission == "+100010") return permission;

            // 删除权限
            permission = permission.Replace("-" + code, "");
            permission = permission.Replace("+" + code, "");
            // 添加权限
            permission = permission + "+" + code;
            return permission;
        }

        // 禁用某个权限，返回新的权限
        static public string Forbidden(string permission, string code)
        {
            // 检测是否为超级管理员
            if (permission == "+100010") return permission;

            // 删除权限
            permission = permission.Replace("-" + code, "");
            permission = permission.Replace("+" + code, "");
            // 添加权限
            permission = permission + "-" + code;
            return permission;
        }

        // 数组转为权限字符串
        static public string ArrayToPermission(string[] codes)
        {
            return "+" + string.Join("+", codes);
        }

        // 获取管理员权限
        static public string GetAdminPermission(string id)
        {
            Admin admin = G.DB.GetById<Admin>(id);
            SqlResult result = G.DB.Get(string.Format("select * from {0} where '{1}' like '%'+convert(varchar,id)+'%'", G.DB.GetTableName<AdminRole>(), admin.role));
            List<AdminRole> roles = result.GetFirstTableList<AdminRole>();
            string permission = "";
            foreach (AdminRole item in roles)
            {
                permission += item.permission;
            }
            return permission + admin.permission;
        }
    }

    public class PermissionOperation
    {
        public PERMISSIONMODULE ModuleCode { get; private set; }
        public PERMISSIONOPERATION OperationCode { get; private set; }
        public PermissionOperation(PERMISSIONMODULE module)
        {
            ModuleCode = module;
        }
        public PermissionOperation Operation(PERMISSIONOPERATION operation)
        {
            OperationCode = operation;
            return this;
        }
        public PermissionOperation Any()
        {
            return Operation(PERMISSIONOPERATION.ANY);
        }
        public PermissionOperation All()
        {
            return Operation(PERMISSIONOPERATION.ALL);
        }
        public PermissionOperation Add()
        {
            return Operation(PERMISSIONOPERATION.ADD);
        }
        public PermissionOperation Delete()
        {
            return Operation(PERMISSIONOPERATION.DELETE);
        }
        public PermissionOperation Modify()
        {
            return Operation(PERMISSIONOPERATION.MODIFY);
        }
        public PermissionOperation Look()
        {
            return Operation(PERMISSIONOPERATION.LOOK);
        }
        public string GetCode()
        {
            return ((int)ModuleCode).ToString() + (OperationCode == PERMISSIONOPERATION.ANY ? "" : ((int)OperationCode).ToString());
        }
        public string GetName()
        {
            return ModuleCode.ToString() + "_" + OperationCode.ToString();
        }
    }

    // 操作码
    public enum PERMISSIONOPERATION
    {
        ANY = 0, // 任意
        ALL = 10, // 所有
        ADD = 11, // 增加
        DELETE = 12, // 删除
        MODIFY = 13, // 修改
        LOOK = 14, // 查看
        EXAMINE = 15, // 审核
        IMPORT = 16, // 导入
        EXPORT = 17, // 导出
        UPLOAD = 18, // 上传

        EXCLUSIVE = 31 // 排他
    }

    // 模块码
    public enum PERMISSIONMODULE
    {
        ALL = 1000,

        // 管理员模块，1-4开头
        AUTHORITY = 1100, // 权限管理,00结尾，为模块组
        ADMIN_ROLE = 1101, // 管理员角色
        MEMBER_ROLE = 1102, // 会员角色
        ADMIN_MANAGE = 1103, // 管理员管理
        MEMBER_MANAGE = 1104, // 会员管理

        // 登陆
        LOGIN = 1201,

        ARTICLE = 1001, // 文章操作

        // 会员模块，5-9开头
        
    }
}